<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of checkpackagecargo
 *
 * @author Chris Sim
 */
class checkpackagecargo extends CValidator {
    //put your code here
    protected function validateAttribute($object,$attribute) {
        $value=$object->$attribute;
        if(!is_array($value))
            $value = array($value);
        $i=0;
        $db = $this->dbConnection;
        $criteria = new CDbCriteria;
        $cargo = array();
        $packing_list = $object->packing_list();
        foreach($packing_list->packages() as $package) {
            $criteria->addCondition("packing_list_package_id=".$package->id, 'or');
        }
        $result = $db->commandBuilder->createFindCommand('cargo_join_packing_package',$criteria)->query();
        while($row = $result->read()) {
            $cargo[$row['cargo_id']] += $row['pkg'];
        }
        foreach($value as $v) {
            $c = cargo::model()->findByPk($c->id);
            $tamt = 0;
            foreach($c->qty() as $_c) {
                $tamt += $_c->qty;
            }
            if($c !== null) {
                $q = $v->pkg + $cargo[$v->id];
                if($q > $tamt) {
                    $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must have at least more than {required} record.');
                    $this->addError($object,$attribute,$message,array('{required}'=>$this->required));
                }
            }
        }
    }
}
?>
